<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            position: absolute;
            left:0;
            top:0;
            background-color: #f00;
            width: 100px;
            height: 100px;
        }

    </style>
</head>
<body>
    <div></div>
    <script>
        var x = 0,y = 0;
        var div = document.querySelector('div');
        document.onkeydown = function(e){
            switch(e.keyCode){
                case 37://left
                    x -= 3;
                    break;
                case 38://up
                    y -= 3;
                    break
                case 39://right
                    x += 3;
                    break
                case 40://down
                    y += 3;
                    break
            }
            div.style.left = x + 'px';
            div.style.top = y + 'px';
            console.log('key:',e.key,'keyCode:',e.keyCode)
        }
    </script>
</body>
</html>